﻿using DevTeam;
using System.Collections.Generic;
using System.Data;
using System.Globalization;

namespace ConfExpert.Forms.ImportWizards.Stok
{
    public partial class Seta_Tip1 : XITopCekiListesi
    {
        int YuklemeId;
        public Seta_Tip1(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            this.Init("Seta Tip1 Top Listesi", true);
            this.FileDone += DestexTip1_FileDone;
            //this.SplitterWidthPercentage = 50;
        }
        
        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                int orderLineStart = -1;
                int orderLineEnd = -1;

                for (int i = 0; i < rows.Count; i++)
                {
                    DataRow dr = rows[i];
                    if (dr[0].ToStr().ToUpper().Contains("CT/NO"))
                        orderLineStart = i + 2;

                    else if (
                        orderLineStart > -1 &&
                        orderLineEnd == -1 &&
                        i > orderLineStart &&

                        dr[0].ToStr().Contains("SUM:")
                        )
                        orderLineEnd = i - 1;
                }

                Dictionary<string, int> indexes = new Dictionary<string, int>();
                if (orderLineStart > 0 && orderLineEnd>0)
                {
                    DataRow dr0 = dt.Rows[orderLineStart - 2];
                    for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                        switch (dr0[colIndex].ToString())
                        {
                            case "COL.NO": indexes.Add("Article", colIndex); break;
                            case "D/": indexes.Add("LotNo", colIndex); break;
                            case "QUANTITY": indexes.Add("Qty", colIndex); break;
                        }


                    for (int i = orderLineStart; i < orderLineEnd + 1; i++)
                    {
                        DataRow dr = rows[i];
                        string patternNo = dr[indexes["Article"]].ToString();

                        if (Article.Select("Article='" + patternNo + "'").Length == 0)
                        {
                            DataRow drArticle = Article.NewRow();
                            drArticle["Article"] = patternNo;
                            Article.Rows.Add(drArticle);
                        }

                        string Qtys = dr[indexes["Qty"]].ToString().Split("=".ToCharArray())[0];
                        foreach (string Qty in Qtys.Split("+".ToCharArray()))
                        {
                            DataRow drRoll = Roll.NewRow();
                            if (indexes.ContainsKey("LotNo"))
                                drRoll["LotNo"] = dr[indexes["LotNo"]];
                            drRoll["Article"] = patternNo;

                            //drRoll["Qty"] = dr[indexes["Qty"]];
                            drRoll["Qty"] = GetDouble(Qty, 0.0d);
                            Roll.Rows.Add(drRoll);
                        }
                    }
                }
            }

            //TODO: peyderpey gelişte, yeni yapıda, ayrımın KALAN kısmını net olarak bilmek gerek... KST'de yeni satır açılıyor ama bunda öyle değil!
        }


        public static double GetDouble(string value, double defaultValue)
        {
            double result;

            //Try parsing in US english
            if (!double.TryParse(value, System.Globalization.NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
                //Then try in the current culture
                !double.TryParse(value, System.Globalization.NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
                //Then in neutral language
                !double.TryParse(value, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out result))
            {
                result = defaultValue;
            }
            return result;
        }


    }
}
